04. Naming Variables

Naming Restrictions

Most programming languages place restrictions on the names that can be used for variables and constants. For example, languages typically restrict you from using a name that is already reserved for a special use by the language—these are called reserved names, reserved words, or keywords.

Here are examples of keywords in Swift: var, let, class, import, private, operator.

A full listing of Swift keywords can be found in The Swift Programming Language: Keywords and Punctuation.

Restrictions on Characters

Also, it is common for programming languages to restrict the characters you can use in a name. In Swift, the following restrictions apply:

  • Names can contain almost any character, including Unicode characters.
  • Names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line-and box-drawing characters.
  • Names must begin with an uppercase or lowercase letter A through Z, an underscore, a non-combining alphanumeric Unicode character in the Basic Multilingual Plane, or a character outside the Basic Multilingual Plane that isn’t in a Private Use Area.

If these statements are hard to follow, don’t worry! For most intents and purposes, we will just be using names with uppercase and lowercase letters A through Z.

Example Names

Here are examples of accepted and unaccepted names in Swift:

// Acceptable Names
 NumberOfYears
_numberOfYears
_numberOfYearsSince1800
valueOf$

// Unacceptable Names
$myVariable
1dollarIsWorth

And if we try these names in a Playground, we can see which names are accepted, and which names cause the Swift compiler to complain.

If a name is invalid, the Swift compiler will display an error.

If a name is invalid, the Swift compiler will display an error.

Naming Conventions

Aside from naming restrictions, there are many conventions used for naming variables and constants. Conventions are used so that we can easily read and understand our code.

Hungarian Notation

One popular convention is Hungarian notation. In Hungarian notation, a name starts with a group of lower-case letters which indicate the type/purpose. The rest of the name—called the given name—is determined by the developer. It is typical for the first character of the given name to be capitalized in order to separate it from the type/purpose letters. For example…

  • intNumberOfLives
  • countNumberOfLives
  • sumTotalScore

Camel Casing

Another convention is CamelCasing. CamelCasing is the practice of writing names such that each word or abbreviation within a name begins with a capital letter. The following examples use CamelCasing…

  • totalCumulativeScore
  • secondsSinceLastUpdate
  • minutesTillLaunch

Naming Constants

When naming constants, a popular convention is to always begin names with a capital letter and then CamelCase the rest of the name. This helps distinguish them from variables. For example…

  • PointsPerLife
  • DefaultGreeting
  • MaxLength

Some even prefer to name constants entirely with capital letters and separate words by underscores. For example…

  • POINTS_PER_LIFE
  • DEFAULT_GREETING
  • MAX_LENGTH

The Most Important Convention

The most important convention you should follow when naming variables or constants is to use a name that effectively communicates the semantic meaning of its value. That is to say, pick names that are unambiguous and clear about the value they represent. For example, if you wanted to represent a value for an hourly wage, the variable name hourlyWage is likely a much better choice than hw or hWage. But, as a developer, as long as you follow naming rules, you have the freedom to choose the verbosity of your names. We'd advise you strike a fair balance between names that are clear and concise.

Literals

When you are reading about variables and constants, you may run into the term literal. A literal is another word for a fixed value.

var myFavoriteNumber = 5
// In the line above, 5 is an Int literal

var salutation = "Hello"
// In the line above, "Hello" is a String literal

Now you may be thinking… a literal sounds like a constant. But, they are different, and this great explanation from a Stack Overflow post makes that clear:

A literal is a value that is written exactly as it's meant to be interpreted. In contrast, a variable is a name that can represent different values during the execution of the program. And a constant is a name that represents the same value throughout a program. But a literal is not a name — it is the value itself.

Why are Variables Useful?

QUESTION:

Now that we've learned about variables, let's pause and review. What are variables? Why are they useful?

ANSWER:

Variables allow you to store values so that you can use the information later in your apps.